<template>
  <div class="site-wrapper site-page--login">

      <div class="login-container">
        <div class="brand-info">
          <h1 class="brand-info__logo">
            <img src="../../assets/img/login-logo.png" alt="">
          </h1>
          <p class="brand-info__intro">Global CPQ syetem</p>
        </div>
        <div class="login-main">
          <header class="login-header">用户登录</header>
          <el-form class="login-form" size="medium" :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" status-icon>
            <el-form-item prop="userName">
              <el-input size="medium" class="input-control" v-model="dataForm.userName" placeholder="工号">
                <template slot="prepend">
                  <span class="icon-input account"></span>
                </template>
              </el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input size="medium" class="input-control" v-model="dataForm.password" type="password" placeholder="密码">
                <template slot="prepend">
                  <span class="icon-input password"></span>
                </template>
              </el-input>
            </el-form-item>
            <el-form-item prop="captcha">
              <el-row :gutter="20">
                <el-col :span="14">
                  <el-input size="medium" class="input-control" v-model="dataForm.captcha" placeholder="验证码">
                    <template slot="prepend">
                  <span class="icon-input pin"></span>
                </template>
                  </el-input>
                </el-col>
                <el-col :span="10" class="login-captcha">
                  <img :src="captchaPath" @click="getCaptcha()" alt="">
                </el-col>
              </el-row>
            </el-form-item>
            <el-form-item>
              <el-button size="medium" class="login-btn-submit" type="danger" @click="dataFormSubmit()">登录</el-button>
            </el-form-item>
          </el-form>
        </div>

        <footer class="login-footer">
          <!-- <div class="title">CPQ-PRM中台项目</div> -->
          <div class="desc">通过微服务框架实现PRM与其他系统之间的对接</div>
          <div class="desc">搭建全球产品数据统一管理后台</div>
        </footer>
      </div>

  </div>
</template>

<script>
  import { getUUID } from '@/utils'
  export default {
    data () {
      return {
        dataForm: {
          userName: '',
          password: '',
          uuid: '',
          captcha: ''
        },
        dataRule: {
          userName: [
            { required: true, message: '工号不能为空', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '密码不能为空', trigger: 'blur' }
          ],
          captcha: [
            { required: true, message: '验证码不能为空', trigger: 'blur' }
          ]
        },
        captchaPath: ''
      }
    },
    created () {
      this.getCaptcha()
    },
    methods: {
      // 提交表单
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl('/prm-os-srv-sys/sys/login'),
              method: 'post',
              data: this.$http.adornData({
                'username': this.dataForm.userName,
                'password': this.dataForm.password,
                'uuid': this.dataForm.uuid,
                'captcha': this.dataForm.captcha
              })
            }).then((data) => {
              if (data && data.code === 0) {
                this.$cookie.set('token', data.token)
                this.$router.replace({ name: 'home' })
              } else {
                this.getCaptcha()
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      // 获取验证码
      getCaptcha () {
        this.dataForm.uuid = getUUID()
        this.captchaPath = this.$http.adornUrl(`/prm-os-srv-sys/captcha.jpg?uuid=${this.dataForm.uuid}`)
      }
    }
  }
</script>

<style lang="scss">
  .site-wrapper.site-page--login {
    height: 100vh;
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    overflow: hidden;
    &:before {
      position: fixed;
      top: 0;
      left: 0;
      z-index: -1;
      width: 100%;
      height: 100%;
      content: "";
      background-image: url(~@/assets/img/login-bg.jpg);
      background-size: cover;
    }

    .login-container {
      width: 364px;
      margin: 0 auto;
      position: absolute;
      top: 55%;
      left: 50%;
      transform: translate(-50%, -50%);
    }

    .brand-info {
      text-align: center;
      color: #fff;
    }
    .brand-info__logo {
      margin: 0 auto;
      width: 182px;
      height: 53px;
      img {
        width: 100%;
        height: 100%;
      }
    }
    .brand-info__intro {
      margin: 10px 0;
      font-size: 18px;
      color:#fff;
    }
    .login-main {
      width: 364px;
      border-radius: 4px;
      background-color: #fff;
    }

    .login-header {
      line-height: 60px;
      text-align: center;
      font-size:18px;
      color:#666;
      border-bottom: 1px solid #e1e8ee;
    }

    .login-captcha {
      padding-left: 0!important;
      padding-right: 0!important;
      overflow: hidden;
      > img {
        width: 100%;
        cursor: pointer;
      }
    }

    .login-form {
      padding: 20px 32px;
    }

    .icon-input {
      display: inline-block;
    }

    .icon-input.account {
      width: 13px;
      height: 17px;
      background:url(~@/assets/img/icons/icon-account.png) no-repeat;
      background-size: contain;
    }

    .icon-input.password {
      width: 12px;
      height: 15px;
      background:url(~@/assets/img/icons/icon-password.png) no-repeat;
      background-size: contain;
    }

    .icon-input.pin {
      width: 15px;
      height: 14px;
      background:url(~@/assets/img/icons/icon-pin.png) no-repeat;
      background-size: contain;
    }

   .login-form :global(.el-input-group__prepend) {
    background-color: transparent;


}

    .login-btn-submit {
      width: 100%;
      margin-top:10px;
    }
  }

  .login-footer {
    margin-top: 50px;
    text-align: center;
    .title {
      line-height: 60px;
      color:#fff;
      font-size: 20px;
    }

    .desc {
      line-height: 20px;
      font-size: 14px;
      color: #999;
    }
  }
</style>
